package com.suneee.cloud.erpInterface.service.impl;

import java.sql.Timestamp;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.suneee.category.dao.CategoryDao;
import com.suneee.category.entity.Category;
import com.suneee.cloud.erpInterface.bean.CategoryBean;
import com.suneee.cloud.erpInterface.bean.ProductBean;
import com.suneee.cloud.erpInterface.service.ErpProductService;
import com.suneee.core.error.Assert;
import com.suneee.core.type.json.JsonUtils;
import com.suneee.store.dao.SkuDao;
import com.suneee.store.dao.SpuDao;
import com.suneee.store.entity.Sku;
import com.suneee.store.entity.Spu;

@Service("api-erpProductService")
public class ErpProductServiceImpl implements ErpProductService{

	private static final Logger logger = LoggerFactory.getLogger(ErpProductServiceImpl.class);
	
	@Autowired
	private SpuDao spuDao;
	
	@Autowired
	private SkuDao skuDao;
	
	@Autowired
	private CategoryDao categoryDao;
	
	@Override
	public void getErpProductInfo(List<ProductBean> productList) {
		Assert .notNull(productList, "商品数据不能为空");
		try {
			logger.info("接收到的ERP商品信息:"+JsonUtils.writer().writeValueAsString(productList));
		} catch (JsonProcessingException e) {
			e.printStackTrace();
		}
		
		if(productList != null && productList.size() > 0){
			for (ProductBean productBean : productList) {
				Spu spu = spuDao.select(Integer.valueOf(productBean.getSpuId()));
				if(spu != null){
					spu.setTitle(productBean.getTitle());
					spu.setCategoryId(productBean.getClassCode());
					spu.setMarketPrice(productBean.getMarketPrice());
					spu.setTotal(productBean.getTotal());
					spu.setUpdateUser(1);
					spu.setUpdateTime(new Timestamp(System.currentTimeMillis()));
					
					spuDao.update(spu);
				}else{
					//新增
					Spu inSpu = new Spu();
					inSpu.setSpuId(Integer.valueOf(productBean.getSpuId()));
					inSpu.setTitle(productBean.getTitle());
					inSpu.setCategoryId(productBean.getClassCode());
					inSpu.setMarketPrice(productBean.getMarketPrice());
					inSpu.setTotal(productBean.getTotal());
					inSpu.setPurchasePrice(0);//采购价字段值待确定
					inSpu.setFromType(1);//企业
					inSpu.setStatus(1);
					inSpu.setCreateTime(new Timestamp(System.currentTimeMillis()));
					inSpu.setCreateUser(1);
					
					spuDao.insert(inSpu);
					
					//生成SKU信息,按照ERP规则1对1
					Sku sku = new Sku();
					sku.setSpuId(inSpu.getSpuId());
					
					Integer skuId= null;
					boolean flag = this.skuPrimary(inSpu.getSpuId() * 1000 + 1);
					if(flag){
						skuId = inSpu.getSpuId() * 1000 + 1;
						break;	
					}
					sku.setSkuId(skuId);
					sku.setStock(inSpu.getTotal());
					sku.setPrice(inSpu.getMarketPrice());//销售价格,
					sku.setStatus(1);
					sku.setCreateTime(new Timestamp(System.currentTimeMillis()));
					sku.setCreateUser(1);
					sku.setPurchasePrice(inSpu.getMarketPrice());
					
					skuDao.insert(sku);
				}
			}
		}
	}
	
	public Boolean skuPrimary(Integer skuId) {
		Sku sku =  skuDao.select(skuId);
		if(sku == null) {
			return true;
		}
		return false;
	}

	@Override
	public boolean getErpCategoryInfo(List<CategoryBean> categoryList) {
		Assert.notNull(categoryList, "类目数据不能为空");
		try {
			logger.info("接收到的ERP类目信息:"+JsonUtils.writer().writeValueAsString(categoryList));
			
			if(categoryList != null && categoryList.size() > 0){
				for (CategoryBean categoryBean : categoryList) {
					//判断是否存在
					Category  ca = categoryDao.select(categoryBean.getClass_code());
					if(ca != null){
						ca.setName(categoryBean.getClass_name());
						categoryDao.update(ca);
					}else{
						Category category = new Category();
						category.setCategoryCode(categoryBean.getClass_code());
						category.setName(categoryBean.getClass_name());
						
						categoryDao.insert(category);
					}
				}
			}
			return true;
		} catch (JsonProcessingException e) {
			e.printStackTrace();
			return false;
		}
	}
	
}
